﻿using AutoMapper;
using Sheep_Breeding.Domain;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Write.Api.DTOSheep;
using Sheep_Breeding.Write.Api.OperationServices.IServices;

namespace Sheep_Breeding.Write.Api.OperationServices.Services
{
    public class SheepFilesServices : ISheepFilesServices
    {
        private readonly IBaseRepository<SheepFiles> sheepFilesRepository;
        private readonly IMapper mapper;
        private readonly ILogger<SheepFilesServices> logger;

        public SheepFilesServices(IBaseRepository<SheepFiles> sheepFilesRepository, IMapper mapper, ILogger<SheepFilesServices> logger)
        {
            this.sheepFilesRepository = sheepFilesRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult> AddSheepFiles(CreateSheepFilesDto createSheepFilesDto)
        {
            try
            {
                logger.LogInformation("新增羊只档案操作");
                var res = new ApiResult();
                var entity = mapper.Map<SheepFiles>(createSheepFilesDto);
                var model = await sheepFilesRepository.AddAsyn(entity);
                if (model > 0)
                {
                    res.success($"羊只档案新增成功");
                    return res;
                }
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError("羊只档案新增报错" + ex.Message);
                throw ex;
            }
        }
    }
}
